# -*- coding: utf-8 -*-

import pygame
import Tile, Pic, Portal

pygame.font.init()

class map_w():
    '''Map-fönstret till MapEditor'''
    def __init__(self,parent):
        # hämtar data från parent
        self.parent = parent
        self.size = self.parent.size[0]-180,self.parent.size[1]-24
        self.nx,self.ny = self.dim = self.parent.dim
        self.pics = self.parent.pics
        self.map = self.parent.map
        self.btn = self.parent.btn
        self.pos = self.parent.r_pos

        # fönstret och markörer
        self.screen = pygame.Surface(self.size)
        self.selection =(self.pos[0]//(22),self.pos[1]//(22))
        self.o_selection = self.selection
        self.marker = pygame.Surface((22,22))
        self.p = lambda s: pygame.font.Font(None,8).render(str(s),True,(0,0,0))

        # ritar ut tiles
        self.screen.fill(pygame.Color('white'))
        for i in self.map:
            tile = self.map[i]
            tile.draw(self.screen,(1+i[0]*(22),1+i[1]*(22)))
            if not tile.okToCross:
                self.screen.blit(self.p('x'),(1+i[0]*(22),1+i[1]*(22)))
            if tile.portalTo:
                self.screen.blit(self.p(str(tile.portalTo)),
                                 (1+i[0]*(22),(i[1]+1)*(22)-7))

    def get_mouse(self):
        # updaterar musens pos och knappar
        self.pos = self.parent.r_pos
        if (self.pos[0] < self.nx*22) and (self.pos[1] < self.ny*22):
            self.o_selection = self.selection
            self.selection =(self.pos[0]//(22),self.pos[1]//(22))
            self.btn = self.parent.btn

    def draw(self):
        # tar bort markören från o_selection
        self.marker.fill(pygame.Color('white'))
        self.screen.blit(self.marker,(self.o_selection[0]*(22),
                                      self.o_selection[1]*(22)))
        tile = self.map[self.o_selection]
        tile.draw(self.screen,(1+self.o_selection[0]*(22),
                               1+self.o_selection[1]*(22))) 
        if not tile.okToCross:
            self.screen.blit(self.p('x'),(1+self.o_selection[0]*(22),
                                          1+self.o_selection[1]*(22)))

        if tile.portalTo:
            self.screen.blit(self.p(str(tile.portalTo)),
                             (1+self.o_selection[0]*(22),
                              (self.o_selection[1]+1)*(22)-7))

        # ritar ut markören på selection
        self.marker.fill(pygame.Color('red'))
        self.screen.blit(self.marker,(self.selection[0]*(22),
                                      self.selection[1]*(22)))
        tile = self.map[self.selection]
        tile.draw(self.screen,(1+self.selection[0]*(22),
                               1+self.selection[1]*(22)))   
        if not tile.okToCross:
            self.screen.blit(self.p('x'),(1+self.selection[0]*(22),
                                     1+self.selection[1]*(22))) 

        if tile.portalTo:
            self.screen.blit(self.p(str(tile.portalTo)),
                             (1+self.selection[0]*(22),
                              (self.selection[1]+1)*(22)-7))

    def loop(self):
        '''loop körs av parent'''
        self.get_mouse()
        if self.btn[0] or self.btn[2]:
            tile = self.map[self.selection]
            i = self.pics.index(str(tile.pic))
            if self.btn[0]: i += 1
            elif self.btn[2]: i -= 1
            i = i%len(self.pics)
            tile.pic = Pic.Pic(self.pics[i]) 
            tile.draw(self.screen,(1+self.selection[0]*(22),
                                   1+self.selection[1]*(22)))
            if not tile.okToCross:
                self.screen.blit(self.p('x'),(1+self.selection[0]*(22),
                                              1+self.selection[1]*(22)))

            if tile.portalTo:
                self.screen.blit(self.p(str(tile.portalTo)),
                                 (1+self.selection[0]*(22),
                                  (self.selection[1]+1)*(22)-7))

        if self.btn[1]:
            tile = self.map[self.selection]
            tile.okToCross = not(tile.okToCross)
            if tile.okToCross:
                tile.draw(self.screen,(1+self.selection[0]*(22),
                                       1+self.selection[1]*(22)))
            else:
                self.screen.blit(self.p('x'),(1+self.selection[0]*(22),
                                              1+self.selection[1]*(22)))
        if self.btn[3]:
            tile = self.map[self.selection]
            if tile.portalTo:
                tile.portalTo = None
                tile.draw(self.screen,(1+self.selection[0]*(22),
                                       1+self.selection[1]*(22)))
            else:
                tile.portalTo = Portal.Portal(0,(0,0))
                self.screen.blit(self.p(str(tile.portalTo)),
                                 (1+self.selection[0]*(22),
                                  (self.selection[1]+1)*(22)-7))
        if self.btn[4]:
            tile = self.map[self.selection]
            print(tile.portalTo)

        if not (self.selection == self.o_selection):
            self.draw()
        return self.selection

    def __str__(self):
        return 'Map'
